
<?php

class kelola_produk {

//connect ke database SmartCommerce


	function __construct(){
			
		$db_config = new db_config();
		
		$link = mysql_connect($db_config->host.':'.$db_config->port, $db_config->db_username, $db_config->db_password);
		
		if (!$link) {
			die('Could not connect: '.mysql_error());
		}
		
		if(!mysql_select_db($db_config->db_name)) {
			die("cannot select db");
		}
		
	}
	
	function select_produk($id_produk){
	
		$sql = '
			SELECT 
				* 
			FROM 
				produk 
			WHERE 
				ID_PRODUK = '.$id_produk.' 
			LIMIT 1
		';
		
		$query = mysql_query($sql);
		
		return $query;
	
	}
	
	function select_produk_by_technopreneur($id_techno){
	
		$sql = '
			SELECT 
				ID_PRODUK, NAMA_PRODUK, HARGA, DESKRIPSI_PRODUK
			FROM 
				produk 
			WHERE 
				ID_TECHNOPRENEUR = '.$id_techno.' 
		';
		
		$query = mysql_query($sql);
		
		return $query;
	
	}
	
	function insert_produk($data){
		
		extract($data, EXTR_OVERWRITE);
		
		$sql = '
			INSERT INTO 
				produk 
				(ID_TECHNOPRENEUR, ID_KATEGORI, NAMA_PRODUK, DESKRIPSI_PRODUK, HARGA, TANGGAL_MASUK_PRODUK) 
			VALUES
				('.$ID_TECHNOPRENEUR.','.$ID_KATEGORI.',"'.$NAMA_PRODUK.'","'.$DESKRIPSI_PRODUK.'",'.$HARGA.',"'.$TANGGAL_MASUK_PRODUK.'")
		';
		
		mysql_query($sql);
		
		$id_produk = mysql_insert_id();
		
		return $id_produk;
		
	}
	
	function update_produk($id_produk, $data){
		
		extract($data, EXTR_OVERWRITE);
		
		$sql = '
			UPDATE 
				produk 
			SET 
				ID_KATEGORI = '.$ID_KATEGORI.', 
				NAMA_PRODUK = "'.$NAMA_PRODUK.'", 
				DESKRIPSI_PRODUK = "'.$DESKRIPSI_PRODUK.'", 
				HARGA = '.$HARGA.' 
			WHERE 
				ID_PRODUK = '.$id_produk.' 
		';
		
		$query = mysql_query($sql);
		
	}
	
	function delete_produk($id_produk){
	
		$sql = '
			DELETE FROM
				produk 
			WHERE
				ID_PRODUK = '.$id_produk.'
		';
		
		mysql_query($sql);
		
	}
	
	function delete_all_produk($id_techno){
	
		$sql = '
			DELETE FROM 
				produk 
			WHERE 
				ID_TECHNOPRENEUR = '.$id_techno.'
		';
		
		mysql_query($sql);
		
	}
	
	function cari_produk($cari){
	
		$sql = '
			SELECT 
				* 
			FROM 
				produk 
			WHERE 
				DESKRIPSI_PRODUK LIKE "%'.$cari.'%" or NAMA_PRODUK LIKE "%'.$cari.'%"
		';
		
		$query = mysql_query($sql);
		
		return $query;
	
	}
	
	function cari_kategori($kategori, $cari){
	
		$sql = '
			SELECT 
				* 
			FROM 
				produk as p left join kategori as q on p.ID_KATEGORI=q.ID_KATEGORI 
			WHERE 
				p.DESKRIPSI_PRODUK LIKE "%'.$cari.'%" or p.NAMA_PRODUK LIKE "%'.$cari.'%" AND p.ID_KATEGORI="'.$kategori.'"
		';
		
		$query = mysql_query($sql);
		
		return $query;
	}
	
	function select_all_produk(){
	
		$sql = '
			SELECT 
				ID_PRODUK, NAMA_PRODUK 
			FROM 
				produk 
			ORDER BY
				ID_PRODUK DESC 
		';
		
		$query = mysql_query($sql);
		
		return $query;
	
	}
}



?>